package com.shuai.controller;

import com.shuai.constant.JwtClaimsConstant;
import com.shuai.constant.LoginTypeConstant;
import com.shuai.dto.PasswordEditDTO;
import com.shuai.dto.UserDTO;
import com.shuai.dto.UserLoginDTO;

import com.shuai.dto.UserPageQueryDTO;
import com.shuai.properties.JwtProperties;

import com.shuai.result.PageResult;
import com.shuai.result.Result;
import com.shuai.service.UserService;
import com.shuai.utils.JwtUtil;
import com.shuai.entity.User;
import com.shuai.vo.UserVO;
import com.shuai.vo.VisualProjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.shuai.vo.UserLoginVO;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);
        User user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ID, user.getId());
        claims.put(JwtClaimsConstant.LOGIN_TYPE, LoginTypeConstant.USER_LOGIN);
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .avatar(user.getAvatar())
                .enterpriseId(user.getEnterpriseId())
                .permission(user.getPermission())
                .name(user.getName())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 新增用户
     * @Param userDTO
     * @return Result
     * */
    @PostMapping("/register")
    @ApiOperation("新增用户")
    public Result save(@RequestBody UserDTO userDTO){

        log.info("新增用户，{}",userDTO);
        userService.save(userDTO);
        return Result.success();
    }



    /**
     *根据id查询用户信息
     * */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户信息")
    public Result<User> getById(@PathVariable Long id){
        log.info("根据id查询用户信息：{}",id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     * */
    @PutMapping()
    @ApiOperation("修改用户信息")
    public Result update(@RequestBody UserDTO userDTO) {
        log.info("修改用户信息：{}", userDTO);
        userService.update(userDTO);
        return Result.success();
    }

    /**
     * 用户分页查询
     * @Param userPageQueryDTO
     * @return Result<PageResult>
     * */
    @GetMapping("/member/page")
    @ApiOperation("分页查询用户")
    public Result<PageResult<UserVO>> page(UserPageQueryDTO userPageQueryDTO){
        log.info("用户分页查询，参数为：{}",userPageQueryDTO);
        PageResult<UserVO> pageResult   = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 列出企业所有员工
     * @Param userPageQueryDTO
     * @return Result<PageResult>
     * */
    @GetMapping("/member/list")
    @ApiOperation("列出企业所有员工")
    public Result<List<UserVO>> listAll( ){
        List<UserVO> userVOList   = userService.listAllUsersInEnterprise();
        return Result.success(userVOList);
    }

    /**
     * 修改密码
     * */
    @PutMapping("/setPassword")
    @ApiOperation("修改密码")
    public Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("修改密码：{}",passwordEditDTO);
        userService.updatePassword(passwordEditDTO);
        return Result.success();
    }

    /**
     * 企业内删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/member/{id}")
    @ApiOperation("删除员工")
    public Result delete(@PathVariable Long id){
        log.info("删除员工：{}",id);
        userService.deleteInEnterpriseWithProjectRelation(id);
        return Result.success();
    }

    /**
     * 根据用户名检索用户
     */
    @PostMapping("/listByUsername")
    @ApiOperation("根据用户名检索用户")
    public Result<List<UserVO>> search(@RequestBody String username){
        log.info("根据用户名检索用户：{}",username);
        List<UserVO> userVOList = userService.listByUsername(username);
        return Result.success(userVOList);
    }


    //TODO 需要实现消息推送
    /**
     * 企业邀请员工加入
     */
    @ApiOperation("企业邀请员工加入")
    @PutMapping("/invite/{id}")
    public Result invite(@PathVariable Long id) {
        //TODO 实现消息发送
        log.info("企业邀请员工加入：{}", id);
        userService.invite(id);
        return Result.success();
    }

    //更换头像
    @PutMapping("/updateAvatar")
    public Result modifyAvatar(@RequestParam  String avatarUrl){
        userService.modifyAvatar(avatarUrl);
        return Result.success();
    }
}
